package com.example.last.controller;

import com.example.last.common.AutoLog;
import com.example.last.common.Result;
import com.example.last.entity.Admin;
import com.example.last.entity.Params;
import com.example.last.service.AdminService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@RequestMapping("/admin")
@RestController
public class AdminController {
    @Resource
    private AdminService adminService;
    private static final Logger log = LoggerFactory.getLogger(AdminController.class);

    @GetMapping("/search")
    public Result findBySearch(@Param("params") Params params){
        log.info("拦截器已放行，接口正常调用");
        PageInfo<Admin> info = adminService.findBySearch(params);
        return Result.success(info);
    }

    //新增信息
    @PostMapping
    public Result save(@RequestBody Admin admin){
        if(admin.getId() == null){
            adminService.add(admin);
        }else{
            adminService.update(admin);
        }
        return Result.success();
    }
    @GetMapping("/username/{username}")
    public Result findByName(@PathVariable String username){
        Admin admin = adminService.findByName(username);
        return Result.success(admin);
    }

    @DeleteMapping("/{id}")
    @AutoLog("删除用户信息")
    public Result delete(@PathVariable Integer id){
        adminService.delete(id);
        return Result.success();
    }


}
